﻿using System;
using System.Collections.Generic;
using System.Text;
using ebay.ebayApi;
using eBay.Service.Core.Soap;
using ebay.Utility;
using System.Drawing;
using System.Data;

namespace ebay.BLL
{
    public class EbayActiveListing
    {
        public void InitEbayActiveListing(string ebayUserId)
        {
            //ebay.Dal.EbayActiveListing active = new ebay.Dal.EbayActiveListing();
            //active.DeleteByEbayUserId(ebayUserId);
        }
        public EbayActiveListing()
        {
        }

        public ebay.Model.EbayActiveListing GetItem(string itemId, string ebayUserId, string token)
        {
            ebayCallsApi apicall = new ebayCallsApi(token);
            ItemType item = apicall.GetItem(itemId);
            if (item == null)
            {
                return null;
            }
            ebay.Model.EbayActiveListing m = new ebay.Model.EbayActiveListing();
            m.Bids = item.SellingStatus.BidCount.ToString();
            m.CategoryLeafName = item.PrimaryCategory.CategoryName;
            m.CategoryNumber = Convert.ToInt32(item.PrimaryCategory.CategoryID);
            m.Condition = item.ConditionDisplayName;
            m.ConditionID = item.ConditionID;
            m.CustomLable = item.SKU;
            m.EndDate = PubClass.GetDateTime(item.ListingDetails.EndTime);
            m.ItemId = item.ItemID;
            m.ItemTitle = PubClass.GetString(item.Title);
            m.Price = item.SellingStatus.CurrentPrice.Value;
            m.PrivateNotes = PubClass.GetString(item.PrivateNotes);
            m.ProductIDType = "";
            m.ProductIDValue = "";//待查
            m.ProductIDValue2 = "";//待查
            m.ProductReferenceID = "";//待查
            m.Purchases = item.Quantity - item.QuantityAvailable;
            m.QuantityAvailable = item.QuantityAvailable;
            m.SiteListed = item.Site.ToString();
            m.StartDate = PubClass.GetDateTime(item.ListingDetails.StartTime);
            m.Type = item.ListingType.ToString();
            if (item.Variations != null)
            {
                foreach (VariationType vt in item.Variations.Variation)
                {
                    m.VariationDetails += vt.VariationTitle + ",";
                }
            }
            if (!string.IsNullOrEmpty(m.VariationDetails))
            {
                m.VariationDetails = m.VariationDetails.Remove(m.VariationDetails.Length - 1);
            }
            else {
                m.VariationDetails = "";
            }
            return m;
        }

        public DataTable GetItemsByProductId(string productId)
        {
            ebay.Dal.EbayActiveListing dalSelling = new ebay.Dal.EbayActiveListing();
            return dalSelling.GetItemsByProductId(productId);
        
        }
    }
}
